<template>
	<detailbox :title="type == 1 ? '重新编辑' : '新增'" @leftclick="close">
		<div slot="right" class="flex_item flex_between">
			<div v-if="type == 1" class="error error_f f12 ml10">
				<i class="f14 el-icon-error"></i>
				审核未通过:
				<span class="ml30">
					{{ checkItem.type == 1 ? '审核人:' : checkItem.type == 3 ? '记录责任人:' : checkItem.type == 4 ? '批准人:' : '' }} {{ checkItem.createUserName }}
				</span>
				<span class="ml30">未通过原因: {{ checkItem.content }}</span>
			</div>
			<div v-else></div>
			<div>
				<el-button type="success" size="small" @click="addFile" v-if="type != 1">添加新增</el-button>
				<el-button v-if="type == 1" type="primary" size="small" plain @click="openDialg('showhistory')">审批历史详情</el-button>
				<el-button type="primary" size="small" @click="submit">提交</el-button>
			</div>
		</div>
		<el-row>
			<el-col :span="18">
				<div style="margin-top:10px;" v-for="(form, index) in formList" :key="index" :id="'item' + index">
					<basic-container>
						<div class="main_title success_f flex_between">
							<b>
								管理信息
								<span v-if="formList.length > 1">({{ index + 1 }})</span>
							</b>
							<el-button type="danger" size="small" plain v-if="index != 0" @click="delFile(index)">删除</el-button>
						</div>
						<el-divider></el-divider>
						<div class="mb20">
							<span class="index_num success_bg">1</span>
							<b class="f14">基础信息</b>
						</div>
						<div>
							<el-form size="small" inline model="form">
								<el-row>
									<el-col :span="12">
										<el-form-item label="生产管理（中间品）编号:" required>
											<el-input v-model="form.number" placeholder="数据带入" class="w160 ml10" readonly v-if="form.numberType == 0"></el-input>
											<el-input class="ml10 w160" placeholder="请输入" v-model="form.number" v-if="form.numberType == 1"></el-input>
											<el-popover placement="top-start" title="编号规则:" width="220" trigger="hover">
												<div>
													<p>SCGLZJP---------生产管理(中间品)</p>
													<p>xxxx---------年月日(当天日期，8位)</p>
													<p>xx---------流水号,从01开始</p>
												</div>
												<i slot="reference" class="el-icon-question f18 gray_f ml10"></i>
											</el-popover>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="生产批号:" required>
											<el-select
												class="w160"
												ref="development"
												no-data-text="没有录入生产计划管理编号"
												v-model="form.batchNumber"
												filterable
												placeholder="请选择"
											>
												<el-option v-for="(item, i) in developmentplanList" :key="i" :label="item.batchNumber" :value="item.batchNumber"></el-option>
											</el-select>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="原材料批号:" required>
											<el-select class="w160" no-data-text="没有录入采购品出库-原材料" v-model="form.materialNumber" filterable placeholder="请选择">
												<el-option v-for="(item, i) in batchNumberList" :key="i" :label="item.batchNumber" :value="item.batchNumber"></el-option>
											</el-select>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="生产日期:" required>
											<el-date-picker
												class="w160"
												v-model="form.productionDate"
												value-format="yyyy-MM-dd"
												type="date"
												placeholder="选择日期"
											></el-date-picker>
										</el-form-item>
									</el-col>
								</el-row>

								<div class="f14 mb20">
									中间品信息
									<el-button size="mini" class="el-icon-circle-plus-outline ml10" type="primary" plain @click="addItem(form)">增加</el-button>
								</div>
								<div class="reviewCon mb20" v-for="(item, i) in form.middleList" :key="i">
									<div class="reviewList">
										<el-row style="position: relative;">
											<el-col :span="8">
												<el-form-item class="m0" label="中间品名称:" required>
													<el-select
														class="w160"
														ref="development"
														v-model="item.middleName"
														filterable
														allow-create
														default-first-option
														placeholder="请输入/创建/搜索"
													>
														<el-option v-for="v in middleList" :key="v.id" :label="v.middleName" :value="v.middleName"></el-option>
													</el-select>
												</el-form-item>
											</el-col>
											<el-col :span="8">
												<el-form-item label="中间品规格型号:" required>
													<el-input class="w160" v-model="item.middleSku" placeholder="请输入"></el-input>
												</el-form-item>
											</el-col>
											<el-col :span="8">
												<el-form-item label="中间品编号:" required>
													<el-input class="w160" placeholder="请输入" v-model="item.middleNumber"></el-input>
													<el-popover placement="top-start" title="推荐编号规则" width="200" trigger="hover">
														<div style="text-align: center;">
															<p>XXXX- -中间品名称首字母</p>
															<p>XXXX--规格型号或专有特性</p>
														</div>
														<i slot="reference" class="el-icon-question f18 gray_f ml10"></i>
													</el-popover>
												</el-form-item>
											</el-col>

											<el-col :span="8">
												<el-form-item label="数量:" required>
													<el-input placeholder="请输入" v-model="item.middleQty" class="w160"></el-input>
												</el-form-item>
											</el-col>
											<el-col :span="8">
												<el-form-item label="单位:" required>
													<el-select v-model="item.unit" class="w160" allow-create default-first-option filterable placeholder="请输入/创建/选择">
														<el-option v-for="v in unitList" :key="v" :label="v" :value="v"></el-option>
													</el-select>
												</el-form-item>
											</el-col>
											<i class="error_f el-icon-delete pointer f20 delete" @click="delItem(form, i, 0)" v-if="i != 0"></i>
										</el-row>

										<!-- 工序 -->
										<el-col :span="24">
											<el-form-item label="是否有特殊过程:" required>
												<el-select placeholder="请选择" v-model="item.isProcess" filterable class="w120" @change="processChange($event,item)">
													<el-option label="否" :value="0"></el-option>
													<el-option label="是" :value="1"></el-option>
												</el-select>
												<template v-if="item.isProcess == 1">
													<el-button size="mini" class="el-icon-circle-plus-outline ml10" type="primary" plain @click="addProcessName(item)">
														增加
													</el-button>
												</template>
												<span class="ml10 f14 main_co">
													特殊过程?
													<el-popover placement="top-start" title="文件编号规则:" width="200" trigger="hover">
														<div class="deepgray_f">QM--------质量手册</div>
														<i slot="reference" class="el-icon-question f16 gray_f"></i>
													</el-popover>
												</span>
											</el-form-item>
										</el-col>

										<div class="reviewCon flex_wrap" v-if="item.isProcess == 1">
											<div class="reviewList mb20 flex_between gongxu" style="width: 46%;" v-for="(v, i) in item.processNamesList" :key="i">
												<el-form-item label="特殊过程名称:" required>
													<el-select v-model="v.name" class="w160" allow-create default-first-option filterable placeholder="请输入/创建/选择">
														<el-option v-for="item in nameList" :key="item" :label="item" :value="item"></el-option>
													</el-select>
												</el-form-item>
												<i class="error_f el-icon-delete pointer f20 delete" @click="delProcessName(item, i)"></i>
											</div>
										</div>
										<div class="mb20">
											<span class="error_f">*</span>
											<span class="f14">生产工序</span>
											<el-button size="mini" class="el-icon-circle-plus-outline ml10" type="primary" plain @click="addgongxuitem(item)">增加</el-button>
											<span class="ml30 f14 main_co">
												关键工序
												<el-popover placement="top-start" title="编号规则" trigger="hover">
													<div class="deepgray_f">XXX--------一般使用P1，P2……</div>
													<i slot="reference" class="el-icon-question f16 gray_f"></i>
												</el-popover>
											</span>
										</div>
										<el-row class="reviewCon">
											<el-col
												:span="11"
												class="reviewList mb20 flex_between gongxu gary_bg"
												:offset="i % 2 == 1 ? '2' : ''"
												style="padding-bottom: 15px;"
												v-for="(v, i) in item.gongxuList"
												:key="i"
											>
												<div>
													<span class="red_f">{{ v.number }}</span>
													<span class="ml30">{{ v.name }}</span>
												</div>
												<div style="margin-right: 30px;">
													<el-button size="small" plan @click="showgongxuitem(item, v, i, 'view')">查看</el-button>
													<el-button size="small" plan @click="showgongxuitem(item, v, i, 'edit')">修改</el-button>
													<i class="error_f el-icon-delete pointer f20 delete " @click="delgongxuitem(item, i)"></i>
												</div>
											</el-col>
										</el-row>
									</div>
								</div>
								<el-row>
									<el-col :span="8">
										<el-form-item label="管理部门:" required>
											<el-cascader
												class="w160"
												placeholder="请选择"
												ref="newdept"
												:options="deptOption"
												v-model="form.manageDeptId"
												:show-all-levels="false"
												:props="{
													value: 'id',
													label: 'deptName',
													emitPath: false,
													checkStrictly: true
												}"
												filterable
												@change="getMemberOption($event, index)"
											></el-cascader>
										</el-form-item>
									</el-col>
									<el-col :span="8">
										<el-form-item label="负责人:" required>
											<el-select placeholder="请选择" v-model="form.manageUserId" filterable class="w160" @change="selectChange($event, form)">
												<el-option v-for="item in form.memberOption" :key="item.id" :value="item.id" :label="item.name"></el-option>
											</el-select>
										</el-form-item>
									</el-col>
									<el-col :span="8">
										<el-form-item label="编制部门:" class="">
											<el-input class="w160" v-model="userInfo.deptName" placeholder="数据带入" readonly></el-input>
										</el-form-item>
									</el-col>
								</el-row>
							</el-form>
						</div>
						<div style="margin-bottom: 10px;"><creat-file :form="form"></creat-file></div>
					</basic-container>
				</div>
			</el-col>
			<el-col :span="6" style="margin-top:10px;"><creat-flow ref="creatFlow" :formList="formList"></creat-flow></el-col>
		</el-row>
		<steps ref="steps" @close="success()" :nowdate="nowdate"></steps>
		<confirm
			ref="confirm"
			@close="save()"
			type="1"
			:guifanFilesProcessRecordList="guifanFilesProcessRecordList"
			:systemGuifanFilesProcessList="systemGuifanFilesProcessList"
		></confirm>
		<showhistory ref="showhistory" type="1" :guifanFilesProcessRecordList="guifanFilesProcessRecordList"></showhistory>
		<add ref="add" :item="currentItem" :Alluser="Alluser" @onConfirm="onConfirm"></add>
		<showgongxu ref="showgongxu" @edit="toEditItem"></showgongxu>
	</detailbox>
</template>

<script>
import add from './add.vue';
import confirm from './confirm.vue';
import showgongxu from './showgongxu.vue';
import { savefiles, getguifanDetail, update, getguifanList } from '@/api/myapi/tixiwenjian';
export default {
	props: {
		detailId: '',
		type: {
			type: [Number, String],
			default: 0
		}
	},
	components: {
		confirm,
		add,
		showgongxu
	},
	data() {
		return {
			developmentplanList: [],
			currentItem: {},
			system: 'productionmanagemiddleprocess',
			checked: false,
			showpreview: false,
			tableData: [],
			batchNumberList: [],
			middleList: [],
			currentList: [],
			currentIndex: 0,
			useTemp: false,
			formList: [
				{
					numberType: 0,
					jiluFileList: [],
					guifanFileList: [],
					memberOption: [],
					middleList: [{}]
				}
			],
			guifanFilesProcessRecordList: [],
			systemGuifanFilesProcessList: []
		};
	},
	created() {
		if (this.detailId) {
			this.getDetail();
		} else {
			this.findTypeByProcessType();
		}
		this.getdeptlist();
		this.getdevelopmentplanList();
		this.getBatchNumberList();
		this.getproductionmanagegongxu();
	},
	methods: {
		//   中间品信息productionmanagemiddleprocessmiddle
		getproductionmanagegongxu() {
			getguifanList(
				{
					current: 1,
					size: 10000,
					isTongguo: 1
				},
				'productionmanagemiddleprocessmiddle'
			).then(({ data }) => {
				console.log(data);
				if (data && data.success) {
					this.middleList = data.data.records;
				}
			});
		},
		addItem(form) {
			if (!form.middleList) this.$set(form, 'middleList', []);
			form.middleList.push({});
		},
		delItem(form, i, type) {
			form.middleList.splice(i, 1);
		},
		onConfirm(item) {
			console.log(item);
			if (!this.currentItem.gongxuList) this.$set(this.currentItem, 'gongxuList', []);
			if (item.type == 'add') {
				this.currentItem.gongxuList.push(item);
			} else {
				let index = this.currentIndex;
				this.currentItem.gongxuList[index] = item;
			}
			this.$refs.add.close();
			this.$refs.add.close();
		},
		toEditItem(item) {
			this.$refs.add.init(item);
		},
		processChange(val,form){
			if(val==0){
				form.processNamesList=[];
			}else{
				if(!form.processNamesList) this.$set(form,'processNamesList',[]);
				form.processNamesList.push({});
			}
		},
		//   工序添加  删除
		addgongxuitem(item) {
			this.currentItem = item;
			this.$refs.add.init();
		},
		showgongxuitem(parent, item, index, type) {
			this.currentItem = item;
			this.currentIndex = index;
			if (type == 'view') {
				this.$refs.showgongxu.init(item);
			} else {
				this.$refs.add.init(item);
			}
		},
		delgongxuitem(form, i) {
			form.gongxuList.splice(i, 1);
		},
		delProcessName(form, i) {
			form.processNamesList.splice(i, 1);
		},
		addProcessName(form) {
			if (!form.processNamesList) this.$set(form, 'processNamesList', []);
			form.processNamesList.push({ name: '' });
		},
		getDetail() {
			getguifanDetail(
				{
					id: this.detailId
				},
				this.system
			).then(({ data }) => {
				console.log(data);
				if (data.success) {
					if (this.type == 2) {
						delete data.data.id;
						data.data.number = '';
						data.data.numberType = 0;
						this.findTypeByProcessType();
					}
					this.formList = [data.data];
					this.formList.forEach(item => {
						this.getuserByDept(item.manageDeptId, item);
						if (item.gongxuList) {
							item.gongxuList.forEach(el => {
								el.operateUserIdList = el.operateUserId && el.operateUserId.split(',');
							});
						}
					});
					this.guifanFilesProcessRecordList = data.data.processRecordList;
					this.checkItem = this.guifanFilesProcessRecordList.find(item => item.checkStatus == 2);
					this.$nextTick(() => {
						this.$refs.creatFlow.setProcessRecord(this.guifanFilesProcessRecordList);
					});
				}
			});
		},
		close() {
			this.$emit('close');
		},
		getMemberOption(val, index) {
			let dept = this.$refs.newdept[index].getCheckedNodes();
			this.formList[index].manageDeptName = dept[0].label;
			this.formList[index].manageDeptId = dept[0].value;
			this.formList[index].manageUserId = '';
			this.formList[index].manageUserName = '';
			this.getuserByDept(val, this.formList[index]);
		},
		//  生产计划管理
		getdevelopmentplanList() {
			getguifanList(
				{
					current: 1,
					size: 10000,
					processStatus: 7
				},
				'productionplanprocess'
			).then(({ data }) => {
				console.log('通过生产计划管理 获取生产批号');
				console.log(data);
				if (data && data.success) {
					this.developmentplanList = data.data.records;
				}
			});
		},
		//  原材料批号 来源采购品出库
		getBatchNumberList() {
			getguifanList(
				{
					current: 1,
					size: 10000,
					purchaseType: 0,
					processStatus: 7
				},
				'purchasechukuprocess'
			).then(({ data }) => {
				console.log('原材料批号 来源采购品出库');
				console.log(data);
				if (data && data.success) {
					this.batchNumberList = data.data.records;
				}
			});
		},
		submit() {
			let systemGuifanFilesProcessList = [];
			let guifanFilesProcessRecordList = this.$refs.creatFlow.guifanFilesProcessRecordList;
			let isOnlineReview = this.$refs.creatFlow.isOnlineReview;
			for (let i = 0; i <= guifanFilesProcessRecordList.length - 1; i++) {
				let item = guifanFilesProcessRecordList[i];
				item.sort=i;
				if (item.type == 1 && !item.userId) {
					this.$message.error('请选择审核人员');
					return;
				}
				if (item.type == 4 && !item.userId) {
					this.$message.error('请选择批准人员');
					return;
				}
				if (isOnlineReview && item.type == 3 && !item.userId) {
					this.$message.error('请选择记录责任人');
					return;
				}
			}
			for (let i = 0; i < this.formList.length; i++) {
				let item = this.formList[i];
				item.processNames = item.processNamesList && item.processNamesList.map(item => item.name).join(',');
				let arr = [
					{
						value: item.number,
						tips: '请补充生产管理编号'
					},
					// {
					//   value: item.planNumber,
					//   tips: '请选择生产计划编号'
					// },
					{
						value: item.materialNumber,
						tips: '请选择原材料批号'
					},
					{
						value: item.productionDate,
						tips: '请选择生产日期'
					},
					{
						value: item.manageDeptId,
						tips: '请选择管理部门'
					},
					{
						value: item.manageUserId,
						tips: '请选择负责人'
					},
					{
						value: item.jiluFilesIds,
						tips: '请添加记录文件'
					},
					{
						value: item.retentionPeriod,
						tips: '请输入记录保留期限'
					},
					{
						value: item.retentionPeriod < 2 ? false : true,
						tips: '记录保留期限应≥2年'
					}
				];
				for (let j = 0; j < arr.length; j++) {
					if (!arr[j].value) {
						this.$message.error(arr[j].tips);
						return;
					}
				}
				item.createDeptName = this.userInfo.deptName;
				item.createUserName = this.userInfo.user_name;
				item.createUserId = this.userInfo.user_id;
				item.createDeptId = this.userInfo.dept_id;
				systemGuifanFilesProcessList.push({
					...item,
					// depts: item.depts.join(','),
					processRecordList: guifanFilesProcessRecordList
				});
			}
			this.systemGuifanFilesProcessList = systemGuifanFilesProcessList;
			this.guifanFilesProcessRecordList = guifanFilesProcessRecordList;
			console.log(this.systemGuifanFilesProcessList);
			console.log(this.guifanFilesProcessRecordList);
			this.openDialg('confirm');
		},
		// 保存
		save() {
			if (this.type == 1) {
				update(this.systemGuifanFilesProcessList, this.system).then(({ data }) => {
					if (data.success) {
						this.$message.success('提交成功');
						this.nowdate = this.dateFormat(new Date());
						this.openDialg('steps');
					}
				});
			} else {
				savefiles(this.systemGuifanFilesProcessList, this.system).then(({ data }) => {
					if (data.success) {
						this.$message.success('提交成功');
						this.nowdate = this.dateFormat(new Date());
						this.openDialg('steps');
					}
				});
			}
		},
		success() {
			this.$emit('success');
		},
		// 发放的文件
		addFile() {
			this.formList.push({
				memberOption: [],
				numberType: this.numberType,
				number: this.numberType == 0 ? this.findTypeByProcessType(this.liushuihao) : '',
				jiluFileList: [],
				guifanFileList: [],
				productList: []
			});
			let index = this.formList.length - 1;
			setTimeout(() => {
				let document = window.parent.document.getElementById(`item${index}`);
				document.scrollIntoView({ behavior: 'smooth' });
			}, 100);
		}
	}
};
</script>

<style scoped="scoped" lang="scss">
.delete {
	position: absolute;
	top: 10px;
	right: 10px;
}
.btnbox {
	border: 1px solid #a3cffd;
	opacity: 1;
	border-radius: 3px 0px 0px 3px;
}

.btnleft {
	padding: 0 10px;
}

.btnright {
	border-left: 1px solid #a3cffd;
	padding: 0 10px;
	line-height: 32px;
}

.border {
	border: 1px solid #dcdfe6;
	border-radius: 4px;
	line-height: 32px;
	padding: 0 10px;
}

.preview {
	padding: 10px;
	background-color: #f0f2f5;
	margin-top: 20px;
	margin-left: 10px;
}

.preview .warn_f {
	background-color: #fbf4e0;
	border: 1px solid #f3dea4;
	line-height: 20px;
	padding: 0 4px;
	margin: 20px 0 10px;
}

.preview .tem {
	width: 377px;
	height: 512px;
}

.preview.bigpreview {
	background-color: #ffffff;
}

.preview.bigpreview .tem {
	width: 510px;
	height: 693px;
}

.error {
	background-color: #fceeec;
	border: 1px solid #f9dcd9;
	padding: 6px 10px;
	border-radius: 2px;
}

div::deep .el-checkbox.is-bordered {
	height: 32px;
	padding: 5px 10px;
}

.fileBox {
	position: relative;
	padding: 20px 10px 1px;
	border: 1px dashed #e4e8ed;

	.del {
		position: absolute;
		right: 10px;
		top: 10px;
	}
}

.worktable {
	td,
	th {
		line-height: 28px;
	}
}

.steps {
	.f16 {
		margin-left: 6px;
	}

	.flex_center {
		margin-bottom: 7px;
	}
}

.btnbox {
	border: 1px solid #a3cffd;
	opacity: 1;
	border-radius: 3px 0px 0px 3px;
}

.btnleft {
	padding: 0 10px;
}

.btnright {
	border-left: 1px solid #a3cffd;
	padding: 0 10px;
	line-height: 32px;
}

.border {
	border: 1px solid #dcdfe6;
	border-radius: 4px;
	line-height: 32px;
	padding: 0 10px;
}

.preview {
	padding: 10px;
	background-color: #f0f2f5;
	margin-top: 20px;
	margin-left: 10px;
}

.preview .warn_f {
	background-color: #fbf4e0;
	border: 1px solid #f3dea4;
	line-height: 20px;
	padding: 0 4px;
	margin: 20px 0 10px;
}

.preview .tem {
	width: 377px;
	height: 512px;
}

.preview.bigpreview {
	background-color: #ffffff;
}

.preview.bigpreview .tem {
	width: 510px;
	height: 693px;
}

.error {
	background-color: #fceeec;
	border: 1px solid #f9dcd9;
	padding: 6px 10px;
	border-radius: 2px;
}

div::deep .el-checkbox.is-bordered {
	height: 32px;
	padding: 5px 10px;
}

.jobfile {
	margin-top: 20px;
	display: flex;
	justify-content: center;
}

.jobLeft {
	width: 34%;
}

.leftTitle {
	height: 48px;
	background: #f5f7fa;
	border: 1px solid #ebf0f5;
	opacity: 1;
	line-height: 48px;
	border-radius: 0px;
	padding: 0 15px;
	font-size: 14px;
	font-family: Microsoft YaHei;
	font-weight: 400;
	color: #303133;
	opacity: 1;
}

.leftList {
	height: 98px;
	background: #ffffff;
	border: 1px solid #ebf0f5;
	opacity: 1;
	line-height: 95px;
	border-radius: 0px;
	display: flex;
	justify-content: flex-start;
	align-items: center;
	padding: 0 15px;
}

.cenList {
	height: 48px;
	background: #ffffff;
	border: 1px solid #ebf0f5;
	opacity: 1;
	line-height: 48px;
	padding: 0 15px;
	display: flex;
	justify-content: flex-start;
	align-items: center;
	border-radius: 0px;
}

.fileBtn {
	margin-left: auto;
	margin-right: 10px;
	font-size: 12px;
	font-family: Microsoft YaHei;
	font-weight: 400;
	line-height: 16px;
	color: #909499;
	opacity: 1;
}

.leftBtn {
	margin-left: auto;
	margin-right: 10px;
}

.leftText {
	font-size: 14px;
	font-family: Microsoft YaHei;
	font-weight: 400;
	line-height: 16px;
	color: #606366;
	opacity: 1;
}

.reviewTitle {
	font-size: 14px;
	font-family: Microsoft YaHei;
	font-weight: bold;
	line-height: 0px;
	color: #606366;
	opacity: 1;
	margin-top: 30px;
}

.addReview {
	width: 74px;
	height: 32px;
	margin: 0 20px;
	background: #e6f1fc;
	border: 1px solid #a3cffd;
	opacity: 1;
	border-radius: 3px;
	font-size: 14px;
	font-family: Microsoft YaHei;
	font-weight: 400;
	line-height: 14px;
	color: #126bc9;
	opacity: 1;
	display: flex;
	justify-content: center;
	align-items: center;
}

.reviewText {
	font-size: 12px;
	font-family: Microsoft YaHei;
	font-weight: 400;
	line-height: 18px;
	color: #909499;
	opacity: 1;
}

.reviewList {
	background: #ffffff;
	border: 1px dashed #e4e8ed;
	padding: 15px 15px 0 15px;
	position: relative;
}
</style>
